<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Music as MusicModel;
use think\Db;
/**
 * 音乐控制器
 * Class Banner
 * @package app\home\controller
 */
class Music extends Base
{
    /**
     * [index  获取禅乐]
     * @param
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new MusicModel;
        $map = [];
        $this->getPageAndSize($param);
        try{
            $map['n.states'] = 1;
            if(!empty($param['title'] )){
                $map['n.title'] = ['like', "%" . $param['title'] . "%"];
            }else{
                $map['n.itemid'] = $param['id'];
            }
            
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,'n.id,n.title,n.img,n.url,n.author');
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $http1 = substr($v['url'],0,4);
                if($http1 != 'http'){
                    $v['url'] = config('domain_name').$v['url'];
                }
                if(empty($v['author'])){
                    $v['author'] = '来自网络';
                }
            }
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [updaown  根据id获取上下数据]
     * @param
     * @return json
     * @author [C。]
     */
    public function updaown()
    {
        $param = input('param.');
        $data = [];
        try{
            $id = $param['id'];
            $data['up'] = Db::query("(SELECT id,title,url,img,author FROM app_music where id < $id and states = 1 order by id desc limit 1)");
            if(!empty($data['up'])){
                $http = substr($data['up'][0]['url'],0,4);
                if($http != 'http') {
                    $data['up'][0]['url'] = config('domain_name') . $data['up'][0]['url'];
                }
                $http = substr($data['up'][0]['img'],0,4);
                if($http != 'http') {
                    $data['up'][0]['img'] = config('domain_name') . $data['up'][0]['img'];
                }
            }


            $data['next'] = Db::query("(SELECT id,title,url,img,author FROM app_music where id > $id and states = 1 limit 1)");
            if(!empty($data['next'])){
                $http = substr($data['next'][0]['url'],0,4);
                if($http != 'http') {
                    $data['next'][0]['url'] = config('domain_name') . $data['next'][0]['url'];
                }
                $http = substr($data['next'][0]['img'],0,4);
                if($http != 'http') {
                    $data['next'][0]['img'] = config('domain_name') . $data['next'][0]['img'];
                }
            }
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'获取成功',$data);
    }
    /**
     * [getmusicbyid  根据id获取禅乐]
     * @param
     * @return json
     * @author [C。]
     */
    public function getmusicbyid()
    {
        $param = input('param.');
        $model = new MusicModel;
        $user = $this->user;
        try{
            $data = $model->findData($param['id'],'id','id,img,title,url,author');
            $http = substr($data['img'],0,4);
            if($http != 'http'){
                $data['img'] = config('domain_name').$data['img'];
            }
            $http1 = substr($data['url'],0,4);
            if($http1 != 'http'){
                $data['url'] = config('domain_name').$data['url'];
            }
            Db::name('music')->where('id', $param['id'])->setInc('play_num');
            if(!empty($user)){
                add_quality($user['id'],32,$user['quality'],4);
            }

        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'获取成功',$data);
    }
}